pub mod card;

use crate::card::Card;

#[allow(unused)]
fn part1(cards: &Vec<Card>) {
    let sum = cards.iter().fold(0, |acc, card| card.points());
    println!("{}", sum);
}

fn part2(cards: &mut Vec<Card>) {
    let mut sum: u32 = 0;
    let mut cards_size = vec![1u32; cards.len()];

    for (i, card) in cards.iter().enumerate() {
        let current_card_size = cards_size.get(i).unwrap().clone();
        sum += current_card_size;

        let point = card.winning_size() as usize;
        if point == 0 {
            continue;
        }

        // Copy cards
        let end = std::cmp::min(i+1+point, cards.len());
        for j in i+1..end {
            if let Some(card_size) = cards_size.get_mut(j) {
                *card_size = *card_size + current_card_size;
            }
        }

    }
    println!("{sum}");
}

fn main() {
    let contents = std::fs::read_to_string("./day04/input.txt").expect("Read file failed.");
    let mut cards: Vec<Card> = contents.lines().map(|line| line.into()).collect();
    part2(&mut cards);
}
